博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
重写Repeater控件,并分页管理
阅读量:6433 次
发布时间:2019-06-23

本文共 9941 字,大约阅读时间需要 33 分钟。

1、重写Repeater(修改过的,当只有一页的情况时,只显示Total页数及共有多少条目,不会显示上一页,下一页等):

View Code
using System.ComponentModel;using System.Web.UI;[assembly: TagPrefix("MyRepeater.Control", "MyRepeater")]namespace MyRepeater{    ///     /// JRepeater控件    ///     [DefaultProperty("Text"), ToolboxData("
<{0}:Repeater ID=\"Rep_List\" runat=server EnableViewState=\"false\" OnPreRender=\"Rep_List_PreRender\">
")] public class Repeater : System.Web.UI.WebControls.Repeater { public bool ShowPagingWhenItemLessThanPageSize { get; set; } private int _recordcount = 0; private int _pagesize = 0; private string _pagelink = ""; private int _currentpage = 1; private string _urlSearchPrex = null; /// /// constructure /// public Repeater() { this.ShowPagingWhenItemLessThanPageSize = true; } #region _ [Bindable(true), Category("Data"), DefaultValue("1"), Description("totalCount")] public int RecordCount { get { return _recordcount; } set { _recordcount = value; } } [Bindable(true), Category("Data"), DefaultValue("1"), Description("pagesize")] public int PageSize { get { return _pagesize; } set { _pagesize = value; } } [Bindable(true), Category("Data"), DefaultValue(""), Description("currentLink")] public string PageLink { get { return _pagelink; } set { _pagelink = value; } } [Bindable(true), Category("Data"), DefaultValue("1"), Description("currentPageIndex")] public int CurrentPage { get { return _currentpage; } set { _currentpage = value; } } public string UrlSearchPrex { get { return _urlSearchPrex; } set { _urlSearchPrex = value; } } #endregion /// /// output control to page /// /// protected override void Render(HtmlTextWriter output) { base.Render(output); if (this._recordcount <= this._pagesize) { output.WriteLine("
Total:1 page(s)   " + this._recordcount + " record(s)
"); return; } if (!(this._recordcount <= this._pagesize && !this.ShowPagingWhenItemLessThanPageSize)) { output.WriteLine("
" + Pagination(_recordcount, _pagesize, _currentpage, _pagelink, _urlSearchPrex) + "
"); } } /// /// paging function /// /// total count /// /// /// ///
public string Pagination(int recordcount, int pagesize, int currentpage, string url, string urlSearchPrex) { int allcurrentpage = 0; int next = 0; int pre = 0; int startcount = 0; int endcount = 0; string currentpagestr = ""; if (currentpage < 1) { currentpage = 1; } //total page count if (pagesize != 0) { allcurrentpage = (recordcount / pagesize); allcurrentpage = ((recordcount % pagesize) != 0 ? allcurrentpage + 1 : allcurrentpage); allcurrentpage = (allcurrentpage == 0 ? 1 : allcurrentpage); } next = currentpage + 1; pre = currentpage - 1; startcount = (currentpage + 5) > allcurrentpage ? allcurrentpage - 9 : currentpage - 4; endcount = currentpage < 5 ? 10 : currentpage + 5; if (startcount < 1) { startcount = 1; } if (allcurrentpage < endcount) { endcount = allcurrentpage; } currentpagestr = "Total: " + allcurrentpage + "  page(s)    " + recordcount + " record(s)      "; string checkaccountName = Page.Request.QueryString["checkaccount"]; string checkprojectName = Page.Request.QueryString["checkproject"]; urlSearchPrex += "&checkaccount=" + checkaccountName + "&checkproject=" + checkprojectName; string urlPrex = url + urlSearchPrex == null ? "?page=" : "?" + urlSearchPrex + "&page="; currentpagestr += currentpage > 1 ? " First      < Prev " : "  First "; for (int i = startcount; i <= endcount; i++) { currentpagestr += currentpage == i ? " " + "[ " + i + " ]" + "" : " " + "[ " + i + " ]" + " "; } currentpagestr += currentpage != allcurrentpage ? "   Next >     Last " : "    Last "; //currentpagestr += "   "; //currentpagestr += "   GO"; return currentpagestr; } }}

2、页面调用方法:

View Code
Emp No. Email ID Role
" name="ckboxChild" style="display: inline" /> <%# Eval("EmpNo") %> <%# Eval("EmailID") %> <%# Eval("Role") %>

后台只需要把数据绑定;

3、常用高效数据库分页存储过程(借用的):

View Code
--高效分页存储过程      CREATE PROCEDURE [dbo].[GetListByPage](   @Table nvarchar(500),          --表名   @Field nvarchar(500) = '*',        --读取字段   @Where  nvarchar(max) = NULL,       --Where条件   @GroupBy nvarchar(500) = NULL,      --分组   @OrderBy nvarchar(500)= NULL,       --排序字段   @PrimaryKeyField nvarchar(50),      --主键必需    @PageIndex int = 1,            --开始页码   @PageSize int = 10,             --页大小   @IsCount bit = 0          --返回记录总数     ------------------------------------------------------------------------------------------------   --当@IsCount为1时,将同时返回2张表,表0为记录总数,表1为查询结果  ------------------------------------------------------------------------------------------------     )   AS   BEGIN       ------------------------------------------------------------------------------------------------       DECLARE @strWhere nvarchar(500)                     --Where 条件       IF @Where IS NOT NULL AND @Where != ''              --Where 条件       BEGIN           SET @strWhere = ' WHERE ' + @Where + ' '       END       ELSE       BEGIN           SET @strWhere = ''       END       ----------------------------------------------------------------------------------------------------       DECLARE @strGroupBy nvarchar(500)                   --GroupBy 条件       IF @GroupBy IS NOT NULL AND @GroupBy != ''          --GroupBy 条件       BEGIN           SET @strGroupBy = ' GROUP BY ' + @GroupBy + ' '       END       ELSE       BEGIN           SET @strGroupBy = ''       END       ----------------------------------------------------------------------------------------------------       DECLARE @strOrderBy nvarchar(500)                   --OrderBy 条件       IF @OrderBy IS NULL OR @OrderBy = ''                --OrderBy 条件       BEGIN           SET @strOrderBy = ' ORDER BY ' + @PrimaryKeyField + ' DESC'       END       ELSE       BEGIN           SET @strOrderBy = ' ORDER BY ' + @OrderBy       END       ----------------------------------------------------------------------------------------------------       DECLARE @strSql nvarchar(max)   --Sql 语句       --计算总行数       IF @IsCount = 1       BEGIN           SET @strSql= 'SELECT  Count (*) AS RecordCount FROM ' + @Table + @strWhere + @strGroupBy           print @strSql          EXEC sp_executesql @strSql           --RETURN        END                  ----------------------------------------------------------------------------------------------------       IF @PageIndex < 1                                  --第一页提高性能       BEGIN              SET @PageIndex = 1       END         IF @PageIndex = 1                                 BEGIN           SET @strSql = 'SELECT TOP ' + str(@PageSize) +  ' ' + @Field + ' FROM ' + @Table +                           @strWhere + @strGroupBy + @strOrderBy           print @strSql          EXEC sp_executesql @strSql           RETURN       END       ----------------------------------------------------------------------------------------------------         DECLARE @STARTID nvarchar(50)       DECLARE @ENDID nvarchar(50)       SET @STARTID = convert(nvarchar(50),(@PageIndex - 1) * @PageSize + 1)       SET @ENDID = convert(nvarchar(50),@PageIndex * @PageSize)       SET @strSql = 'WITH MYTABLE AS (SELECT ROW_NUMBER() OVER (' + @strOrderBy + ')                      AS RowNumber,' + @Field + ' FROM '+ @Table +  @strWhere + @strGroupBy + ')                       SELECT * FROM MYTABLE                       WHERE RowNumber BETWEEN ' + @STARTID + ' AND ' + @ENDID       print @strSql      EXEC sp_executesql @strSql       --------------------------------------------------------------------------------------------------   END

4、有上面的存储过程的使用,同样可以在后台实现真分页

 

转载于:https://www.cnblogs.com/52life/archive/2013/03/08/2949286.html

你可能感兴趣的文章
Java静态块学习
查看>>
基础命令cd
查看>>
openstack 删除僵尸实例
查看>>
常见的以太网电缆
查看>>
一篇、【MariaDB】日志审计
查看>>
Android 轻松实现语音识别
查看>>
kylin(麒麟)分页遇到的问题
查看>>
C++对析构函数的误解
查看>>
RequestMapping注解 ant使用说明
查看>>
tcp/ip socket http 区别
查看>>
fstab损坏修复过程总结
查看>>
linux系统安装(CentOS 6.5)
查看>>
对象不支持“startsWith”属性或方法
查看>>
java提高篇(六)-----关键字static
查看>>
Activiti(四)创建一个最简单的activiti项目
查看>>
HttpWatch v10.x发布,支持在Firefox 35-35版中使用HTTP/2丨附下载
查看>>
Easy Keygen练习
查看>>
微服务随笔
查看>>
限流 RateLimiter
查看>>
idea控制台进行彩色打印
查看>>